//
//  BottomBounceView.m
//  Common
//
//  Created by shiqing guo on 2018/10/11.
//  Copyright © 2018年 www.bitkinetic.com. All rights reserved.
//

#import "BottomBounceView.h"
#import "UIView+Category.h"
#import "UIView+DDAddition.h"
#import "EPColor.h"
#import "Macro.h"

@interface BottomBounceView ()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, assign) BOOL hasGesture;
@end
@implementation BottomBounceView

- (instancetype)initWithFrame:(CGRect)frame withHasGesture:(BOOL)hasGesture {
    if (self = [super initWithFrame:frame]) {
        self.hasGesture = hasGesture;
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews {
    self.backgroundColor = [UIColor clearColor];
    // 设置背景的透明度渐变
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.bgView gradualTransparency];
    [self addSubview:self.bgView];
    [self addSubview:self.btnMore];
    self.btnMore.bottom = self.height - 23.0f - IPhoneX_FixHeight;
    self.btnMore.centerX = self.width/2;
}

#pragma mark - 懒加载
- (UIButton *)btnMore
{
    if (!_btnMore) {
        _btnMore = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 120.0f, 24.0f)];
        _btnMore.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [_btnMore setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnMore setBackgroundColor:[EPColor epColor_Blue]];
        [_btnMore addTarget:self action:@selector(btnMoreClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnMore;
}

- (void)btnMoreClick:(id)sender
{
    [self dismissView];
    if (self.clickBlock) self.clickBlock();
}

- (void)showView {
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:self];
    
    if (self.hasGesture) {
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)]];
    }

    [window addSubview:self];
    
    self.transform = CGAffineTransformMakeTranslation(0.01, UI_SCREEN_WIDTH);
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0.01, 0.01);
    }];
}

- (void)dismissView {
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0.01, UI_SCREEN_WIDTH);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
